package com.lj.ljminsu.wk.controller;

import com.lj.ljminsu.wk.bean.*;
import com.lj.ljminsu.wk.service.*;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("create")
public class controller {
    @Resource
    private ICreateHouseService iCreateHouseService;
    @Resource
    private IAddBedMsgService iAddBedMsgService;
    @Resource
    private IAddRoomNumsService iAddRoomNumsService;
    @Resource
    private IAddTTMService iAddTTMService;
    @Resource
    private IAddPriceRuleService iAddPriceRuleService;
    @Resource
    private IAddServService iAddServService;
    @Resource
    private IAddPicService iAddPicService;
    @Resource
    private IAddMiaoshuService iAddMiaoshuService;
    @Resource
    private IAddMsIdService iAddMsIdService;


    @RequestMapping("createHouse")
    @ResponseBody
    public JsonResultVo createHouse(CreateHouseBean postData) {
        //将页面传回来的地址信息转为字符串
        String address = postData.getCountry() + "-" + postData.getProvince() + "-" +
                postData.getCity() + "-" + postData.getArea() + "-" + postData.getStreet() + "-" +
                postData.getXiaoqu() + "-" + postData.getBuild() + "-" + postData.getUnit() + "-" +
                postData.getDoor() + "-" + postData.getLng() + "-" + postData.getLat();
        //将页面传回来额地址信息包装到数据库对象中
        AddHouseBean addHouseBean = new AddHouseBean();
        addHouseBean.setUserid(1);
        addHouseBean.setAddress(address);

        //iCreateHouseService.addHouse(postData);
        iCreateHouseService.addHouse(addHouseBean);//添加房屋记录
        //System.out.println(addHouseBean.getHouseid());//返回的主键id,(就是房屋id)


        String houseid = Integer.toString(addHouseBean.getHouseid());

        JsonResultVo jsonResultVo = new JsonResultVo();
        jsonResultVo.setCode(1);//为1则跳转
        jsonResultVo.setMsg(houseid);//将房屋id添加进去


//        System.out.println(postData);

        return jsonResultVo;

    }

    @RequestMapping("step2")
    @ResponseBody
    public JsonResultVo step2(Step2Bean postData) {

//        System.out.println(postData);

        //获取房屋id
        Integer houseid = postData.getRoomid();

        //获取数据中的床的信息
        String beds = postData.getBeds();
        List<BedMsg> begMsgList = new ArrayList<>();
        Integer bedTotleNums = 0;

        String[] split = beds.split("#");
        for (int i = split.length - 1; i >= 0; i--) {
//            System.out.println(split[i]);
            String[] split1 = split[i].split(",");
            BedMsg bedMsg = new BedMsg();
            bedMsg.setBedType(Integer.parseInt(split1[0]));
            bedMsg.setBedLength(split1[1]);
            bedMsg.setBedWidth(split1[2]);
            bedMsg.setBedNum(Integer.parseInt(split1[3]));
            bedMsg.setHouseid(houseid);
            begMsgList.add(bedMsg);
            bedTotleNums += Integer.parseInt(split1[3]);
        }
//        System.out.println(begMsgList);
//        System.out.println(bedTotleNums);


        //获取房间数量信息
        RoomsNums roomsNums = new RoomsNums();
        roomsNums.setBathroom(postData.getBathroomSelf() + postData.getBathroomPublic());
        roomsNums.setBedroom(postData.getBedroom());
        roomsNums.setHouseid(postData.getRoomid());
        roomsNums.setLivingroom(postData.getLivingroom());
        roomsNums.setKitchenroom(postData.getKitchenroom());
        roomsNums.setSameroom(postData.getSameroom());
        roomsNums.setSize(postData.getSize());
        roomsNums.setHouseType(postData.getHouseType());


        //获取房屋名称和出租类型和最大入住人数信息

        TitleAndTypeAndMaxNum TTM = new TitleAndTypeAndMaxNum();

        TTM.setHouseid(houseid);
        TTM.setMaxNum(postData.getMaxNum());
        TTM.setRentType(postData.getRentType());
        TTM.setTitle(postData.getTitle());
        TTM.setBedTotleNums(bedTotleNums);


//        System.out.println(roomsNums);

        /*添加床型*/
        iAddBedMsgService.addBedMsgDao(begMsgList);

        /*添加房间数量*/
        iAddRoomNumsService.addRoomNums(roomsNums);

        /*添加名称出租类型和最大可入住人数*/
        iAddTTMService.addTTM(TTM);


        JsonResultVo jsonResultVo = new JsonResultVo();
        jsonResultVo.setCode(1);
        jsonResultVo.setMsg(Integer.toString(houseid));

        return jsonResultVo;

    }

    @RequestMapping("step3")
    @ResponseBody
    public JsonResultVo step3(Step3Bean postData) {

        iAddPriceRuleService.addPriceRule(postData);


//        System.out.println(postData);
        JsonResultVo jsonResultVo = new JsonResultVo();
        jsonResultVo.setCode(1);
        jsonResultVo.setMsg(Integer.toString(postData.getRoomid()));

        return jsonResultVo;

    }


    @RequestMapping("step4")
    @ResponseBody
    public JsonResultVo step4(Step4Bean postData) {

//        System.out.println(postData);

        iAddServService.addServ(postData);
        JsonResultVo jsonResultVo = new JsonResultVo();
        jsonResultVo.setCode(1);
        jsonResultVo.setMsg(Integer.toString(postData.getRoomid()));

        return jsonResultVo;

    }

    @RequestMapping("step5")
    @ResponseBody
    public JsonResultVo step5(Step5Bean postData) {


//        Integer userid = 1;
        Integer houseid = postData.getRoomid();

        List<Pic> stringList = new ArrayList<>();
        Pic pic1 = new Pic();
        Pic pic2 = new Pic();
        Pic pic3 = new Pic();
        pic1.setPic(postData.getPic1());
        pic1.setHouseid(houseid);
        pic2.setPic(postData.getPic2());
        pic2.setHouseid(houseid);
        pic3.setPic(postData.getPic3());
        pic3.setHouseid(houseid);

        stringList.add(pic1);
        stringList.add(pic2);
        stringList.add(pic3);


        /*添加图片*/
        iAddPicService.addPic(stringList);


        /*添加描述*/
        Miaoshu miaoshu = new Miaoshu();
        miaoshu.setFyms(postData.getFyms());
        miaoshu.setCclx(postData.getCclx());
        miaoshu.setZbss(postData.getZbss());
        miaoshu.setJzdb(postData.getJzdb());
        miaoshu.setSqms(postData.getSqms());

        iAddMiaoshuService.addMiaoshu(miaoshu);

//        System.out.println(miaoshu.getMiaoshuId());
        /*将该描述id添加到房屋表中*/

        Integer MsId = miaoshu.getMiaoshuId();

        iAddMsIdService.addMsId(MsId,houseid);


//        System.out.println(postData);
        JsonResultVo jsonResultVo = new JsonResultVo();
        jsonResultVo.setCode(1);
        jsonResultVo.setMsg(Integer.toString(postData.getRoomid()));

        return jsonResultVo;

    }


    /*图片上传*/

    @RequestMapping("upload")
    @ResponseBody
    public JsonResultVo uploadImages(MultipartFile file) {
        JsonResultVo jsonResultVo = new JsonResultVo();

        try {
//            byte[] bytes = file.getBytes();
            InputStream inputStream = file.getInputStream();
            String filename = file.getOriginalFilename();

            try {
                FTPClient ftp = new FTPClient();
                //连接FTP服务器，默认端口是21
                ftp.connect("192.168.71.86", 21);
                //匿名用户必须使用anonymous登录，密码是邮箱
                boolean login = ftp.login("anonymous", "1756229479@qq.com");
                System.out.println(login);

                //获取ftp服务器的响应
                int replyCode = ftp.getReplyCode();
                if (!FTPReply.isPositiveCompletion(replyCode)) {
                    System.out.println("获取响应失败");
                    jsonResultVo.setCode(1);
                    return jsonResultVo;
                }
                //设置接收文件类型为二进制文件
                ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
                //服务器上创建images文件夹
                ftp.makeDirectory("image");
                //切换ftp默认文件夹
                ftp.changeWorkingDirectory("image");

                //将本地图片上传到ftp服务器上
                String remoteFileName = System.currentTimeMillis() + filename;
                ftp.storeFile(remoteFileName, inputStream);
                //退出登录
                ftp.logout();

                jsonResultVo.setCode(0);
                jsonResultVo.setMsg("http://192.168.71.86:80/image/" + remoteFileName);
            } catch (IOException e) {
                e.printStackTrace();
            }


        } catch (MaxUploadSizeExceededException e) {
            jsonResultVo.setCode(1);
            jsonResultVo.setMsg("超过最大上传限制500KB");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonResultVo;
    }

    /*退出登录*/
    @CrossOrigin
    @ResponseBody
    @RequestMapping("out-login")
    public Integer outLogin() {
        Integer state = 1;
        return state;
    }

}
